//
//  HomeViewController.m
//  项目一TimeMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 huangmingyu. All rights reserved.
//

#import "HomeViewController.h"
#import "TableViewCell.h"
#import "HomeModel.h"
#import "PostView.h"


@interface HomeViewController () {
    
    BOOL flag;
    __weak IBOutlet PostView *postView;
    __weak IBOutlet UITableView *listView;
    
    NSMutableArray *_modelArr;
}

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //加载数据
    [self loadData];
    
    
    postView.hidden = YES;
    postView.backgroundColor = self.view.backgroundColor;
    listView.backgroundColor = [UIColor colorWithWhite:0.32 alpha:1];
        self.title = @"电影";
    [self creatCustomView];
    
    
    postView.dataArray = _modelArr;
}



- (void)loadData {
    
    _modelArr = [NSMutableArray array];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"us_box" ofType:@"json"];
    
    NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
    
    id result = [NSJSONSerialization JSONObjectWithData:jsonData
                                                options:NSJSONReadingMutableContainers
                                                  error:nil];
    NSArray *subjects = result[@"subjects"];
    for (NSDictionary *dic in subjects) {
        
        NSDictionary *subject = dic[@"subject"];
        
        HomeModel *model = [[HomeModel alloc] init];
        model.tiltle = subject[@"title"];
        model.rating = subject[@"rating"];
        model.EnglishTiltle = subject[@"original_title"];
        model.year = subject[@"year"];
        model.images = subject[@"images"];
        [_modelArr addObject:model];
    }
    
    postView.dataArray = _modelArr;
    
    
}

- (void)creatCustomView {
    
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    customView.backgroundColor = [UIColor clearColor];

    
    UIButton *customButton = [[UIButton alloc] init];
    [customButton setImage:[UIImage imageNamed:@"poster_home@2x"] forState:UIControlStateNormal];
    [customButton setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home@2x"] forState:UIControlStateNormal];
    [customButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    customButton.frame = customView.frame;
    
    customButton.backgroundColor = [UIColor clearColor];
    [customView addSubview:customButton];
    
    UIButton *listButton = [[UIButton alloc] init];
    [listButton setImage:[UIImage imageNamed:@"list_home@2x"] forState:UIControlStateNormal];
    [listButton setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home@2x"] forState:UIControlStateNormal];
    [listButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    listButton.frame = customView.frame;
    listButton.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.1];
    [customView addSubview:listButton];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
    
    postView.hidden = YES;
    
    
}


- (void)buttonAction:(UIButton *)button {
    
    
    
    UIView *customView = self.navigationItem.rightBarButtonItem.customView;
    [customView sendSubviewToBack:button];
//    flag = !flag;
    
    UIViewAnimationTransition transition = postView.hidden ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight;
    listView.hidden = postView.hidden;
    postView.hidden = !listView.hidden;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [UIView setAnimationTransition:transition forView:customView cache:YES];
//        self.view.backgroundColor = flag ? [UIColor redColor]:[UIColor greenColor];
        
    }];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [UIView setAnimationTransition:transition forView:self.view cache:YES];
//        self.view.backgroundColor = flag ? [UIColor redColor]:[UIColor greenColor];
        
    }];
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil] firstObject];
        
//        cell.backgroundColor = [UIColor clearColor];
        
        cell.cellModel = _modelArr[indexPath.row];
        
        
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return kScreenWidth / 3;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
